		.386
		option	segment:use16, proc:private

		include	strings.a

		echo	StrDelM, StrDelMStk
	

$segStrings	segment	para public 'Strings'
		assume	cs:$segStrings, ds:nothing


; strdelm -	Deletes characters from a string.
;
; inputs:
;		ES:DI- Points at the string to delete characters from.
;		CX - Index into source string (ES:DI) to begin deletion.
;		AX - Number of characters to delete.
; outputs:
;		ES:DI -	Points at new string on stack which is the image
;		of the source string minus the specified characters.
;
;		Carry=1 if memory allocation error, 0 if sufficient
;		memory for the new string.
;
; StrDelMStk	Number of character to delete, the index into the source
;		string, and the pointer to the string are passed on the
;		stack in that order.


$StrDelM	proc	far
		push	es
		push	di
		push    si
		push	ds
		push	ax
		push	bx
		push	cx
		mov	bx, ax			;Save length

; Compute the length of the source string:

		push	es
		push	di
		push	cx
		mov	cx, 0ffffh
		mov	al, 0
	repne	scasb
		neg	cx
		sub	cx, bx			;Compute length of new str.
		jnc	DoAlloc			;Too much to delete?
		pop	cx			;Use insertion point as
		push	cx			; the length.

DoAlloc:	call	sl_malloc
		jnc	GoodAlloc
		add	sp, 6
		pop	cx
		pop	bx
		pop	ax
		pop	ds
		pop	si
		pop	di
		pop	es
		ret

GoodAlloc:	pop	cx
		pop	si
		pop	ds
		push	es			;Save ptr to new string.
		push	di
Cpy1:		lodsb
		stosb
		cmp	al, 0
		loopne	Cpy1
		jz	DelDone
Skp1:		mov	cx, bx			;Get # chars to delete
Skp2:		lodsb
		cmp	al, 0
		loopne	Skp2
		jz	DelDone
Cpy2:		lodsb
		stosb
		cmp	al, 0
		jnz	Cpy2

DelDone:	mov	byte ptr es:[di], 0
		pop	di
		pop	es
		pop	cx
		pop	bx
		pop	ax
		pop	ds
		pop	si
		add	sp, 4			;Don't restore es:di
		ret
$StrDelM	endp


$StrDelMSTk	proc	far
		push	bp
		mov	bp, sp
		push	ax
		push	cx

		mov	ax, [bp+12]
		mov	cx, [bp+10]
		les	di, [bp+6]
		call	$StrDelM

		pop	cx
		pop	ax
		pop	bp
		ret	8
$StrDelMStk	endp


stdlib		ends
		end
